<?php
/**
 * Created by PhpStorm.
 * User: priestll
 * Date: 20-7-14
 * Time: 下午3:05
 */

namespace App\Biz;


use App\Model\UserModel;
use EasySwoole\ORM\DbManager;
use EasyWeChat\Factory;
use priestll\easyswoole\biz\BaseBiz;
use priestll\easyswoole\util\Token;
use priestll\easyswoole\wechat\WeChatManager;

class UserBiz extends BaseBiz
{
    public $token;

    public function test(){

        return 'ok';
    }

    /**
     * 账号密码登陆
     */
    public function login(){

    }

    /**
     * 手机登陆
     */
    public function loginByMobile($mobile){
        return $this->fail('登陆失败');
        $user = UserModel::create()
                ->field([
                    'id',
                    'username',
                    'name',
                    'nickname',
                    'avatar',
                    'openid',
                    'unionid',
                    'wx_openid',
                    'mini_openid',
                    'mobile',
                ])
                ->get([
                    'mobile'=>$mobile
                ]);
        if($user){
            $token = Token::getInstance();
            $str = $token->create($user->toArray());
            if (!$str)
            {
                return $this->fail('登陆失败');
            }
            return $this->success($str);
        }else{
            return $this->fail('登陆失败');
        }
    }

    /**
     * 微信H5登陆
     */
    public function loginByH5($code){
        try
        {
            if (!$code)
            {
                return $this->fail("missing code!");
            }
            $user=$this->getTokenUser("id",$code);
            if (!$user)
            {
                $user =$this->getTokenUser("mobile",13827215564);
            }
            if($user)
            {
                $member=$user->toArray();
                $token = Token::getInstance();
                $this->token = $token->create($member);
            }
            else
            {
                return $this->fail('登陆失败');
            }
            return $this->success(["token"=>$this->token,"round"=>$member["round"],"id"=>$user["id"]]);
        }
        catch (\Exception $e)
        {
            return $this->fail('登陆失败:'.$e->getMessage());
        }

    }

    /**
     * 微信H5登陆
     */
    public function loginByH5Online($code){
        try
        {
            if (!$code)
            {
                return $this->fail("missing code!");
            }
            $weChat=WeChatManager::getInstance()->weChat('wechat');
            $jsApi = $weChat->officialAccount()->jsApi();
            $member = $jsApi->auth()->codeToUser($code);
            $user =$this->getTokenUser("openid",$member->getOpenid());

            if($user)
            {
                $token = Token::getInstance();
                $this->token = $token->create($user->toArray());
            }
            else
            {
                $model = new UserModel();
                $model->setAttr('nickname', $member->getNickname());
                $model->setAttr('openid',$member->getOpenid());
                $model->setAttr('avatar',$member->getHeadimgurl());
                $model->setAttr('wx_openid', $member->getOpenid());
                $res = $model->save();
                if (!$res)
                {
                    return $this->fail('登陆失败!!');
                }
                $user=$this->getTokenUser("id",$model->id);
                $token = Token::getInstance();
                $this->token = $token->create($user->toArray());
            }
            if (empty($this->token))
            {
                return $this->fail('登陆失败');
            }
            return $this->success(["token"=>$this->token,"round"=>$user->round,"id"=>$user["id"]]);
        }
        catch (\Exception $e)
        {
            return $this->fail('登陆失败:'.$e->getMessage());
        }

    }

    /**
     * 微信小程序登陆
     */
    public function loginByMiniprogram(){

    }

    public function update($id,$mobile,$name)
    {
        $user = UserModel::create()->where($id)->get();
        if (!$user)
        {
            return $this->fail("没有该用户");
        }
        $count = UserModel::create()->where(["mobile"=>$mobile])->where("id <> {$id}")->limit(1)->count();
        if ($count>0)
        {
            return $this->fail("该手机号码已注册");
        }
        $res=$user->update(["modile"=>$mobile,"name"=>$name]);
        if (!$res)
        {
            return $this->fail("修改失败");
        }
        return $this->success($res);


    }

    public function getTokenUser($field,$val)
    {
        return UserModel::create()
            ->field([
                'id',
                'username',
                'name',
                'nickname',
                'avatar',
                'openid',
                'unionid',
                'wx_openid',
                'mini_openid',
                'mobile',
                'round',
                'award_times'
            ])
            ->get([
                $field=>$val
            ]);
    }

    public function jssdk($url)
    {
        $weChat=WeChatManager::getInstance()->weChat('wechat');
        $jsApi = $weChat->officialAccount()->jsApi();
        $jsApiSignaturePack = $jsApi->sdk()->signature($url);
        $jsApiSignaturePack->getAppId();
        $jsApiSignaturePack->getNonceStr();
        $jsApiSignaturePack->getSignature();
        $jsApiSignaturePack->getTimestamp();
        return $this->success($jsApiSignaturePack);

    }


}